<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_ecommerce'.DS.'tables');	

class EcommerceModelKatalog extends JModel
{
	var $_product = NULL;
	var $_where = NULL;
	var $_result = 0;
	var	$_sotrang = 0;
	var $_tht = 0;
	var $_page = NULL;
	var $_type = NULL;
	
	function __construct()
	{
		global $mainframe;
		parent::__construct();
		$type = intval(JRequest::getVar('type', 0));
		$this->_type = $type;
	}
	function getList()
	{
		if(!$this->_product)
		{		
			global $mainframe;
			$this->_params = &$mainframe->getParams();
									
			if(!(JRequest::getVar('page',0))){
				$this->_tht = 1;
			} else if(JRequest::getVar('page') == 'all'){
				$this->_tht = 0;
			} else {
				$this->_tht = (int)JRequest::getVar('page');
			}
			
			$query = "SELECT product_id FROM #__auction WHERE closed = 0";
			$this->_db->setQuery($query);
			$proid = $this->_db->loadResultArray();
			$strid = implode(',', $proid);
			$where = " AND p.id IN (".$strid.")";
			
			//get total record
			$query = "SELECT count(p.id) FROM #__pr_product p WHERE published = 1 " . $where ;
			$this->_db->setQuery( $query );
			//tong so record
			$total = $this->_db->loadResult();
			$this->_result = $total;//print_r($total);exit();
			
			if($this->_tht){
				$sodong = 20;// so record trong 1 trang
			} else {
				$sodong = 500;
			}
			$this->_sotrang = ceil($total/$sodong);
			if($this->_tht){
				$vitridau = ($this->_tht - 1 )*$sodong;
			} else {
				$vitridau = 0;
			}
			
			
			$query = "SELECT p.*, a.id as auc_id, a.currentprice FROM #__pr_product as p INNER JOIN #__auction as a ON p.id = a.product_id WHERE a.published = 1 " . $where . " ORDER BY p.katno LIMIT ".$vitridau.",".$sodong;
			//print_r($query);exit();
			$this->_db->setQuery($query);	
			$this->_product = $this->_db->loadObjectList();//print_r($this->_product); exit();
			
			return $this->_product;
		}
		//return $this->_product;
	}
	
	
	function getSotrang(){
		return $this->_sotrang;
	}
	
	function getTotalResult(){
		return $this->_result;
	}

	function getTrangHienTai(){
		return $this->_tht;
	}	
}
?>